package pl.darbat.incomecalc.view;

import java.util.List;
import java.util.Map;

import pl.darbat.incomecalc.R;
import pl.darbat.incomecalc.R.id;
import pl.darbat.incomecalc.R.layout;
import pl.darbat.incomecalc.input.PhoneDigitsKeyListener;
import pl.darbat.incomecalc.system.SimpleAdapter;
import android.content.Context;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView.OnEditorActionListener;

public class MonthsAdapter extends SimpleAdapter {
	
	OnEditorActionListener mOnEditorActionListener;
	OnKeyListener mOnKeyListener;
	PhoneDigitsKeyListener mInputFilter;
	private int mResource;

	public MonthsAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		mResource = resource;
		mInputFilter = new PhoneDigitsKeyListener(false,true);
	}
	
	public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) {
		mOnEditorActionListener = onEditorActionListener;
	}
	
	public void setOnKeyListener(OnKeyListener onKeyListener) {
		mOnKeyListener = onKeyListener;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = super.getView(position, convertView, parent);
		MonthsViewHolder mvh;
		if(convertView == null){
			mvh = new MonthsViewHolder();
			if(mResource == R.layout.month_row) {
				mvh.input = (EditText) v.findViewById(R.id.monthIncomeBrutto);
				mvh.isBrutto = true;
			} else {
				mvh.input = (EditText) v.findViewById(R.id.monthIncomeNetto);
				mvh.isBrutto = false;
			}
			//mvh.input.setOnEditorActionListener(mOnEditorActionListener);
			mvh.input.setOnKeyListener(mOnKeyListener);
			mvh.input.setKeyListener(mInputFilter);
		} else {
			mvh = (MonthsViewHolder) v.getTag();
		}
		mvh.position = position;
		v.setTag(mvh);
		return v;
	}
}
